$(function() {
    var AppUtil = {
        androidUrl: "", // android下载地址
        iosUrl: "", // ios下载地址
        locOrigin: "", // 后台访问地址
        type: "", // 当前机型

        init: function() {
            this.locOrigin = location.origin;
            if (this.locOrigin.indexOf("http") == -1) {
                this.locOrigin = "http://jrh.stock.pingan.com";
            }
            this.type = navigator.userAgent.toUpperCase();
            this.getAppDownLoadAddr(this.type);
            this.initDisplay();
            this.bind();
        },

        initDisplay: function() {
            var params = this.getUrlParam();
            if (/ipad/i.test(this.type)) {
                // IPAD
                $(".ipad-page").show();
                $(".phone-page").hide();

                if (!params.url) {
                    // 隐藏打开E掌通按钮
                    $(".ipad-open").hide();
                } else {
                    $(".ipad-open").show();
                }
            } else {
                $(".phone-page").show();
                $(".ipad-page").hide();

                if (!params.url) {
                    // 隐藏打开E掌通按钮
                    $(".phone-open").hide();
                } else {
                    $(".phone-open").show();
                }
            }
        },

        bind: function() {
            var self = this;
            if (/ipad/i.test(this.type)) {
                // 下载
                $(".ipad-down").on("click", function() {
                    self.download();
                });

                // 打开
                $(".ipad-open").on("click", function() {
                    self.open();
                });
            } else {
                // 下载
                $(".phone-down").on("click", function() {
                    self.download();
                });

                // 打开
                $(".phone-open").on("click", function() {
                    self.open();
                });
            }
        },

        // 是否微信客户端
        weixin: function() {
            var ua = navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == "micromessenger") {
                //显示提示内容
                $(".weixin").slideDown();
                setTimeout(function() {
                    $(".weixin").slideUp();
                }, 5000);
                alert("请点击右上角,在新的浏览器下载App");
                return true;
            } else {
                return false;
            }
        },

        // 下载
        download: function() {
            if (this.weixin()) {
                return;
            }
            if (
                this.type.indexOf("IPHONE OS") != -1 ||
                this.type.indexOf("IPAD") != -1
            ) {
                this.downIos();
            } else {
                this.downAndroid();
            }
        },

        // 下载android
        downAndroid: function() {
            this.saveDownTimes("android", this.androidUrl);
        },

        // 下载IOS
        downIos: function() {
            this.saveDownTimes("ios", this.iosUrl);
        },

        // 打开
        open: function() {
            if (this.weixin()) {
                return;
            }
            if (this.type.indexOf("IPHONE OS") != -1) {
                this.openIos();
            } else if (this.type.indexOf("IPAD") != -1) {
                this.openIpad();
            } else {
                this.openAndroid();
            }
        },

        // 打开IOS
        openIos: function() {
            var params = this.getUrlParam();
            window.location.href =
                "com.pingan.eztpro://" +
                (params.url ? "?url=" + params.url : "");
        },

        // 打开Ipad
        openIpad: function() {
            var params = this.getUrlParam();
            window.location.href =
                "com.pingan.hdproezt://" +
                (params.url ? "?url=" + params.url : "");
        },

        // 打开android
        openAndroid: function() {
            var t1 = Date.now();
            var params = this.getUrlParam();
            var ifr = document.createElement("iframe");
            ifr.setAttribute(
                "src",
                "com.pingan.eztpro://fc.app/openwith" +
                    (params.url ? "?url=" + params.url : "")
            );
            ifr.setAttribute("style", "display:none");
            document.body.appendChild(ifr);
            // alert('open');
            // window.location.href ="com.pingan.eztpro://fc.app/openwith" +
            //         (params.url ? "?url=" + params.url : "");
            setTimeout(function() {
                var t2 = Date.now();
                alert('t2-t1='+(t2-t1));
                // if (!t1 || t2 - t1 < 1030) {
                //     // 当前没有此app
                //     alert("请先安装App");
                // }
                document.body.removeChild(ifr);
            }, 1000);
        },

        // 获取ios,android的下载地址
        getAppDownLoadAddr: function(device) {
            var self = this;
            var addrUrl =
                self.locOrigin +
                "/fc/do/app/user/getAppDownLoadAddr.do?time=" +
                new Date().getTime();
            $.ajax({
                url: addrUrl,
                dataType: "json",
                success: function(res) {
                    try {
                        if (/ipad/i.test(device)) {
                            self.iosUrl = res.resultBody.ipad.replace(
                                /\s/g,
                                ""
                            );
                        } else {
                            self.iosUrl = res.resultBody.iosOrg.replace(
                                /\s/g,
                                ""
                            );
                        }
                    } catch (e) {
                        self.iosUrl = "";
                    }
                    if (
                        !self.startWith(self.iosUrl, "itms-services") &&
                        self.endWith(self.iosUrl, ".plist")
                    ) {
                        self.iosUrl =
                            "itms-services://?action=download-manifest&url=" +
                            self.iosUrl;
                    }
                    try {
                        self.androidUrl =
                            res.resultBody.android +
                            "?time=" +
                            new Date().getTime();
                    } catch (e) {
                        self.androidUrl = "";
                    }
                },
                error: function() {
                    alert("获取下载路径失败");
                    return;
                }
            });
        },

        //保存平台下载量
        saveDownTimes: function(type, downUrl) {
            console.log(this.locOrigin);
            $.ajax({
                url:
                    this.locOrigin +
                    "/fc/do/app/user/clientDownLoadCnt.do?time=" +
                    new Date().getTime(),
                data: {
                    sourceType: this.getParameter("platform"),
                    deviceType: type
                },
                dataType: "json",
                complete: function() {
                    console.log("2=" + downUrl);
                    if (downUrl) {
                        downUrl = downUrl.replace(/\s/g, "");
                        location.href = downUrl;
                    } else {
                        alert("获取下载路径无效");
                        return;
                    }
                }
            });
        },

        //获取url参数
        getParameter: function(param) {
            var reg = new RegExp("[&,?]" + param + "=([^\\&]*)", "i");
            var value = reg.exec(location.search);
            return value ? value[1] : "";
        },

        getUrlParam: function() {
            var url = location.search;
            var theParam = new Object();
            if (url.indexOf("?") != -1) {
                var str = url.substr(1);
                strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    //遍历参数数组
                    theParam[strs[i].split("=")[0]] = unescape(
                        strs[i].split("=")[1]
                    );
                }
            }
            return theParam;
        },

        //判断是否以某字符串结束
        endWith: function(str, endStr) {
            var len = str.length - endStr.length;
            return len >= 0 && str.lastIndexOf(endStr) == len;
        },

        //判断是否以某字符串开始
        startWith: function(str, startStr) {
            var state = str.indexOf(startStr);
            if (state == 0) {
                return true;
            } else {
                return false;
            }
        }
    };

    AppUtil.init();
    // window.AppUtil = AppUtil;
});
